home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / ExtensionFileFilter.java < prev    next >
Text File  |  1998-10-26  |  2KB  |  112 lines

  1. package com.symantec.itools.swing.filechooser;
  2.  
  3.  
  4. import java.io.File;
  5. import java.util.Enumeration;
  6. import java.util.Hashtable;
  7. import com.sun.java.swing.preview.filechooser.FileFilter;
  8.  
  9.  
  10. public class ExtensionFileFilter
  11.     extends FileFilter
  12. {
  13.     protected String    desc;
  14.     protected boolean   allowDirs;
  15.     protected Hashtable extensions;
  16.     protected boolean   allowAll;
  17.  
  18.     {
  19.         extensions = new Hashtable();
  20.     }
  21.  
  22.     public ExtensionFileFilter(boolean f)
  23.     {
  24.         allowDirs = f;
  25.     }
  26.  
  27.     public void addExtension(String ext, boolean f)
  28.     {
  29.         if(f)
  30.         {
  31.             ext = ext.toLowerCase();
  32.         }
  33.  
  34.         if(!(extensions.containsKey(ext)))
  35.         {
  36.             extensions.put(ext, new Boolean(f));
  37.         
  38.             if(ext.equals("*") || ext.equals("*.*"))
  39.             {
  40.                 allowAll = true;
  41.             }
  42.         }
  43.     }
  44.  
  45.     public boolean accept(File file)
  46.     {
  47.         int    index;
  48.         String ext;
  49.         String name;
  50.         
  51.         // is it a directory?
  52.         if(file.isDirectory())
  53.         {            
  54.             return (allowDirs);
  55.         }
  56.         
  57.         // handle "*" & "*.*"
  58.         if(allowAll)
  59.         {
  60.             return (true);
  61.         }
  62.         
  63.         name  = file.getName();
  64.         index = name.lastIndexOf('.');
  65.  
  66.         // is there even an extension?
  67.         if(index == -1)
  68.         {
  69.             return (false);
  70.         }
  71.  
  72.         ext = name.substring(index + 1);
  73.  
  74.         // is it a match right off?
  75.         if(extensions.containsKey(ext))
  76.         {
  77.             return (true);
  78.         }
  79.  
  80.         for(Enumeration e = extensions.keys(); e.hasMoreElements();)
  81.         {
  82.             String filterExt;
  83.  
  84.             filterExt = (String)e.nextElement();
  85.  
  86.             if(((Boolean)extensions.get(filterExt)).equals(Boolean.FALSE))
  87.             {
  88.                 if(filterExt.equals(".*"))
  89.                 {
  90.                     return (true);
  91.                 }
  92.                 
  93.                 if(filterExt.equalsIgnoreCase(ext))
  94.                 {
  95.                     return (true);
  96.                 }
  97.             }
  98.         }
  99.         
  100.         return (false);
  101.     }
  102.  
  103.     public void setDescription(String str)
  104.     {
  105.         desc = str;
  106.     }
  107.  
  108.     public String getDescription()
  109.     {
  110.         return (desc);
  111.     }
  112. }